layui.config({
    base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', "form"], function () {
    $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    $("#cateName").focus(function () {
        layer.open({
            type: 2,
            area: ['400px', '500px'],
            maxmin: true,
            moveOut: false,
            shade: 0.4,
            async: true,
            title: '选择课程分类',
            content: "/courseCate/tree"
        });
    });

    window.cateTreeCallBack = function (node) {
        if (node.isSubCate) {
            $("#cateName").val(node.name);
            $("input[name='cateId']").val(node.id);
            $("input[name='cateIdPath']").val(node.catePath);
            return {isClose: true};
        }
        return {
            isClose: false,
            callback: function () {
                layer.msg("只能选择子分类");
            }
        };
    };

    form.verify({
        studyDay: [
            /^[0-9]*[1-9][0-9]*$/
            , '输入正确的课程有效期'
        ],
        passPercent: [
            /^[0-9]*[1-9][0-9]*$/
            , '课程百分比 '
        ],
        originalPrice: [
            /^([1-9][0-9]*)+(.[0-9]{1,2})?$/
            , '课程原价 '
        ],
        salePrice: [
            /^([1-9][0-9]*)+(.[0-9]{1,2})?$/
            , '课程售卖价 '
        ]
    });
    form.on("submit(save)", function (data) {
        $.post("/course/edit/save", $('.layui-form-pane').serialize(), function (res) {
            if (res.code == "00000") {
                layer.msg("保存成功", {
                    icon: 6, time: 1500, end: function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                });
            } else {
                console.log(res);
            }
        });
        return false;
    });

});